﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSDClient.Service;
using Newtonsoft.Json;
using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib.Models;
using PSDLib.Ctrl;

namespace PSDClient
{
    public partial class PSDClientDevice : UserControl
    {
        public DownloadHelper DownloadHelper { get; set; }

        public dl_download_device Device { get; set; }

        public PSDClientDeviceState DeviceState
        {
            get
            {
                return (PSDClientDeviceState)Enum.Parse(typeof(PSDClientDeviceState), lblDownloadState.Text);
            }
            set
            {
                lblDownloadState.Text = value.ToString();
            }
        }
        public PSDClientDevice()
        {
            InitializeComponent();

            this.palDeviceName.Visible = false;

            SetDownloadState(false);
            SetBurnState(false);
        }

        public void SetDevice(dl_download_device device)
        {
            Device = device;

            if (Device != null)
            {
                this.lblDeviceName.Text = Device.device_name;
                this.lblDeviceIP.Text = Device.device_ip;

                palDisk.Controls.Clear();

                //if (!string.IsNullOrEmpty(Device.main_storage))
                //{
                //    SetStorage(Device.main_storage, Device.main_storage_total, Device.main_storage_free, 1);
                //}

                //if (!string.IsNullOrEmpty(Device.second_storage))
                //{
                //    SetStorage(Device.second_storage, Device.second_storage_total, Device.second_storage_free, 2);
                //}

                foreach (var storage in device.StorageList)
                {
                    SetStorage(storage);
                }
            }
        }

        public void SetDownloadState(bool online)
        {
            if (online)
            {
                this.lblDownloadState.Text = "启动";
                this.lblDownloadState.ForeColor = Color.Green;
            }
            else
            {
                this.lblDownloadState.Text = "停止";
                this.lblDownloadState.ForeColor = Color.Gray;
            }
        }

        public void SetBurnState(bool online)
        {
            if (online)
            {
                this.lblBurnState.Text = "启动";
                this.lblDownloadState.ForeColor = Color.Green;
            }
            else
            {
                this.lblBurnState.Text = "停止";
                this.lblDownloadState.ForeColor = Color.Gray;
            }
        }


        private void SetStorage(dl_download_device_storage storage)
        {
            try
            {
                DiskInfo diskInfo = new DiskInfo();
                palDisk.Controls.Add(diskInfo);
                diskInfo.Dock = DockStyle.Left;
                diskInfo.BringToFront();
                diskInfo.DiskDriver = storage.storage_driver;
                diskInfo.VolumeName =storage.storage_name;
                diskInfo.SumSpace = storage.storage_total_size;
                diskInfo.FreeSpace = storage.storage_free_size;
                diskInfo.StorageType = storage.storage_type;
                diskInfo.Group = storage.storage_group;
            }
            catch { }
        }

        private void picEditDeviceName_Click(object sender, EventArgs e)
        {
            if (palDeviceName.Visible)
            {
                txtDeviceName_Leave(null, null);
            }
            else
            {
                txtDeviceName.Text = lblDeviceName.Text;
                palDeviceName.Visible = true;
            }
        }

        private void txtDeviceName_Leave(object sender, EventArgs e)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("deviceId", DownloadHelper.UUID);
            data.Add("deviceName", this.txtDeviceName.Text.Trim());

            var result = HttpHelper.Post<HResult>(Setting.API.SetDeviceName, data);

            if (result.success)
            {
                this.lblDeviceName.Text = this.txtDeviceName.Text.Trim();
            }

            this.palDeviceName.Visible = false;
        }
    }

    public enum PSDClientDeviceState : int
    {
        未启动 = 0,
        正在下载 = 1,
        正在刻录 = 2,
    }
}
